﻿using System;
using System.Net;

using System.Collections.Generic;
using System.Linq;
using Tulde.Core;

namespace Tulde.MP3tunes
{
    public enum OutputTypes
    {
        json,
        xml
    }
    public enum ServiceTypes
    {
        artist,
        album,
        track,
        playlist,
        video,
        locker
    }
    public enum ServiceKeys
    {
        artist_id,
        album_id,
        track_id,
        playlist_id
    }
    public enum ResultDetail
    {
        max,
        min
    }
    public class ServiceParameters 
    {
        protected T GetAsType<T>(string parameter, Func<string, T> parser = null)
        {
            if (parser == null)
            {
                try
                {
                    return (T)Convert.ChangeType(parameter, typeof(T),null);
                }
                catch
                {
                    return default(T);
                }
            }
            else
            {
                return parser(parameter);
            }
        }
        private Dictionary<string, string> parameters = new Dictionary<string, string>();
        protected void SetParameter( string key, string value)
        {
            if (this.parameters.ContainsKey(key))
            {
                this.parameters[key] = value;
            }
            else
            {
                this.parameters.Add(key, value);
            }
        }
        public IEnumerable<string> Keys
        {
            get
            {
                return this.parameters.Keys.AsEnumerable();
            }
        }
        public string Remove( string key)
        {
            if (this.parameters.ContainsKey(key))
            {
                string r = this.parameters[key];
                this.parameters.Remove(key);
                return r;
            }
            else
            {
                return null;
            }
        }
        public string GetParameter(string key)
        {
            if (this.parameters.ContainsKey(key))
            {
                return this.parameters[key];
            }
            else
            {
                return null;
            }
        }

        public string SessionId
        {
            get
            {
                return this.GetParameter(Constants.SessionId);
            }
            set
            {
                this.SetParameter(Constants.SessionId, value);
            }
        }
        public string PartnerToken
        {
            get
            {
                return this.GetParameter(Constants.PartnerToken);
            }
            set
            {
                this.SetParameter(Constants.PartnerToken, value);
            }
        }
        public OutputTypes OutputType
        {
            get
            {
                return (OutputTypes)Enum.Parse(typeof(OutputTypes), this.GetParameter(Constants.Output),true);
            }
            set
            {
                this.SetParameter(Constants.Output, value.ToString());
            }
        }

        public ServiceParameters()
        {
            this.PartnerToken = SecurityProvider.Credentials.Token;
            this.DefaultParameters();
        }
        protected virtual void DefaultParameters()
        {
            this.OutputType = OutputTypes.json;
            
        }
    }
    public class DataServiceParameters : ServiceParameters
    {
        
        public ServiceTypes ServiceType
        {
            get
            {
                return (ServiceTypes)Enum.Parse(typeof(ServiceTypes), this.GetParameter(Constants.Type),true);
            }
            set
            {
                this.SetParameter(Constants.Type, value.ToString());
            }
        }
        public string PlaylistServiceKey
        {
            get
            {
                return this.GetParameter(ServiceKeys.playlist_id.ToString());
            }
            set
            {
                this.SetParameter(ServiceKeys.playlist_id.ToString(), value);
            }
        }
        public string ArtistServiceKey
        {
            get
            {
                return this.GetParameter(ServiceKeys.artist_id.ToString());
            }
            set
            {
                this.SetParameter(ServiceKeys.artist_id.ToString(), value);
            }
        }
        public string AlbumServiceKey
        {
            get
            {
                return this.GetParameter(ServiceKeys.album_id.ToString());
            }
            set
            {
                this.SetParameter(ServiceKeys.album_id.ToString(), value);
            }
        }

        public bool MiniData
        {
            get
            {
                return this.GetAsType<bool>(this.GetParameter(Constants.MiniData));
            }
            set
            {
                this.SetParameter(Constants.MiniData, value.ToString().ToLower());
            }
        }    
        

        public DataServiceParameters()
        {
            this.SessionId = SecurityProvider.Token.SessionID;
            this.MiniData = false;
           // this.SetParameter("result_data_level","max");
            
        }
    }
    public class DataServiceSearchParameters : DataServiceParameters
    {
        public string SearchPattern
        {
            get
            {
                return this.GetParameter(Constants.SearchPattern);
            }
            set
            {
                this.SetParameter(Constants.SearchPattern, value);
            }
        }
        public ResultDetail ResultDataLevel
        {
            get
            {
                return (ResultDetail)Enum.Parse(typeof(ResultDetail), this.GetParameter(Constants.ResultDataLevel),true);
            }
            set
            {
                this.SetParameter(Constants.ResultDataLevel, value.ToString());
            }
        }
       
        public DataServiceSearchParameters()
        {
            this.ResultDataLevel = ResultDetail.min;
            
        
        }
    }
    public class PlayerServiceParameters : ServiceParameters
    {
       
        /**
         * fileformat (optional): Forces the file to be transcoded and streamed in mp3 format when set to "mp3". Otherwise the file is returned in it's original format. If the MIME type for the file is "video/mp4" or "video/quicktime", the fileformat parameter for lockerPlay will accept "h264" as a valid value and return the video stream.
bitrate (optional): Forces the file to be transcoded to a specified bitrate. valid options are: 28000, 56000, 96000, 128000, or 192000
         * */
        public string FileKey
        {
            get
            {
              return   this.GetParameter(Constants.FileKey);
            }
            set
            {
                this.SetParameter(Constants.FileKey, value);
            }
        }
        public string FileFormat
        {
            get
            {
                return this.GetParameter("fileformat");
            }
            set
            {
                this.SetParameter("fileformat", value);
            }
        }
        public Bitrate Bitrate
        {
            //get
            //{
            //    return this.GetParameter("bitrate");
            //}
            //set
            //{
            //    this.SetParameter("bitrate", value);
            //}
             get
            {
                return (Bitrate)Int32.Parse(this.GetParameter("bitrate"));
            }
            set
            {
                this.SetParameter("bitrate", ((int)value).ToString());
            }
        }
        public string FileName
        {
            get
            {
                return this.GetParameter("ext");
            }
            set
            {
                this.SetParameter("ext", value);
            }
        }

        public PlayerServiceParameters()
        {
           
        }
        protected override void DefaultParameters()
        {
            this.SessionId = SecurityProvider.Token.SessionID;
           // this.SetParameter("ext", ".mp3");
        }
    }
    public class LockerStatParameters : ServiceParameters
    {
        public string User
        {
            get
            {
                return this.GetParameter(Constants.User);
            }
            set
            {
                this.SetParameter(Constants.User, value);
            }
        }
        public string Count
        {
            get
            {
                return this.GetParameter(Constants.Count);
            }
            set
            {
                this.SetParameter(Constants.Count, value);
            }
        }
        
        
        public LockerStatParameters()
        {
            this.SetParameter(Constants.Filter, "recently_played");
            this.SetParameter(Constants.Type, "track");
            this.OutputType = OutputTypes.json;
        }
    }
    public class AuthenticationServiceParameters : ServiceParameters
    {
        public AuthenticationServiceParameters()
        {

        }
        public string UserName
        {
            get
            {
                return this.GetParameter(Constants.UserName);
            }
            set
            {
                this.SetParameter(Constants.UserName, value);
            }
        }
        public string Password
        {
            get
            {
                return this.GetParameter(Constants.Password);
            }
            set
            {
                this.SetParameter(Constants.Password, value);
            }
        }
    }
}
